<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Navs;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class NavsController extends CommonController
{
    public function index()
    {
        
        $data= Navs::orderBy('navs_order','asc')->get();
        return view('admin.navs.index',compact('data'));
    }
    //友情链接排序
    public function changeOrder()
    {

        $input=Input::all();
        $navs=Navs::find($input['navs_id']);
        $navs->navs_order=$input['navs_order'];
        $res=$navs->update();
        if($res){
            $data=[
                'status'=>0,
                'msg'=>'友情链接排序更新成功',
            ];
        }else{
            $data=[
                'status'=>1,
                'msg'=>'友情链接排序更新失败',
            ];
        }
        return $data;
    }


    //get 方法 admin/navs/create 路由 添加友情链接
    public function create()
    {
        return view('admin.navs.add');
    }

    //post方法 admin/navs 路由 添加链接  提交
    public function store()
    {
        //打印除token值以外的数据
        if($input=Input::except('_token')) {
                //dd($input);
            $rules = array(
                'navs_name' => 'required ',
                'navs_url' => 'required ',
            );
            $mes=array(
                'navs_name.required'=>'导航名称不能为空',
                'navs_url.required'=>'导航地址不能为空',
            );

            $validator = Validator::make($input, $rules, $mes);

            if ($validator->passes()) {
                $res=Navs::create($input);
                if($res){
                    return redirect('admin/navs');
                }else{
                    return back()->with('errors','自定义导航填加失败，请稍后重试！');
                }
            } else {
                return back()->withErrors($validator);
            }
        }

    }

    //get 方法  admin/navs/{navs}/edit 路由文章编辑
    public function edit($navs_id)
    {
        $filed=Navs::find($navs_id);
        return view('admin.navs.edit',compact('filed'));
    }

    //PUT 方法 admin/navs/{navs} 路由
    public function update($navs_id)
    {
        $input=Input::except('_token','_method');
        $res=Navs::where('navs_id',$navs_id)->update($input);

        if($res){
            return redirect('admin/navs');
        }else{
            return back()->with('errors','友情链接更新失败，请稍后重试！');
        }

    }
    //delete 方法 admin/navs/{navs} 路由 删除友情链接
    public function destroy($navs_id)
    {
        $re = Navs::where('navs_id',$navs_id)->delete();
        if($re){
            $data = [
                'status' => 0,
                'msg' => '友情链接删除成功！',
            ];
        }else{
            $data = [
                'status' => 1,
                'msg' => '友情链接删除失败，请稍后重试！',
            ];
        }
        return $data;
    }

    //get 方法 admin/navs/{navs} 路由
    public function show()
    {

    }

}
